home *** CD-ROM | disk | FTP | other *** search
/ Champak 43 / Vol 43.iso / games / phit.swf / scripts / __Packages / CTrayBuilder.as < prev    next >
Encoding:
Text File  |  2007-07-13  |  3.8 KB  |  129 lines

  1. class CTrayBuilder
  2. {
  3.    var _arrGroups;
  4.    var _desiredGroupSize;
  5.    var _minGroupSize;
  6.    var _maxGroupSize;
  7.    var _tileGrid;
  8.    var _trayExtentX = 0;
  9.    var _trayExtentY = 0;
  10.    function CTrayBuilder()
  11.    {
  12.    }
  13.    function Build(trayExtentX, trayExtentY, minGroupSize, maxGroupSize)
  14.    {
  15.       this._arrGroups = new Array();
  16.       this._desiredGroupSize = new Array();
  17.       if(minGroupSize == undefined)
  18.       {
  19.          minGroupSize = 1;
  20.       }
  21.       if(maxGroupSize == undefined)
  22.       {
  23.          maxGroupSize = 6;
  24.       }
  25.       this._minGroupSize = minGroupSize;
  26.       this._maxGroupSize = maxGroupSize;
  27.       this._trayExtentX = trayExtentX;
  28.       this._trayExtentY = trayExtentY;
  29.       this.RandomizeTray();
  30.       return this._arrGroups;
  31.    }
  32.    function RandomizeTray()
  33.    {
  34.       this._tileGrid = new Array();
  35.       var _loc3_ = 0;
  36.       while(_loc3_ < this._trayExtentY)
  37.       {
  38.          var _loc2_ = 0;
  39.          while(_loc2_ < this._trayExtentX)
  40.          {
  41.             this._tileGrid[_loc3_ * this._trayExtentX + _loc2_] = -1;
  42.             _loc2_ = _loc2_ + 1;
  43.          }
  44.          _loc3_ = _loc3_ + 1;
  45.       }
  46.       this.CreatePieces();
  47.    }
  48.    function CreateNewGroup()
  49.    {
  50.       this._arrGroups.push(new Array());
  51.       return this._arrGroups.length - 1;
  52.    }
  53.    function CreatePieces()
  54.    {
  55.       FreshDebug.Assert(this._tileGrid != undefined,"_tileGrid != undefined");
  56.       var _loc4_ = 0;
  57.       while(_loc4_ < this._trayExtentY)
  58.       {
  59.          var _loc2_ = 0;
  60.          while(_loc2_ < this._trayExtentX)
  61.          {
  62.             if(this._tileGrid[_loc4_ * this._trayExtentX + _loc2_] < 0)
  63.             {
  64.                var _loc3_ = this.CreateNewGroup();
  65.                FreshDebug.Assert(_loc3_ >= 0,"iGroup >= 0");
  66.                this.EstablishGroup(new Vector2D(_loc2_,_loc4_),_loc3_);
  67.             }
  68.             _loc2_ = _loc2_ + 1;
  69.          }
  70.          _loc4_ = _loc4_ + 1;
  71.       }
  72.    }
  73.    function CompareNeighborsRandomly(v1, v2)
  74.    {
  75.       return !_root.random.GetBoolean() ? 1 : -1;
  76.    }
  77.    function AddToGroup(tile, iGroup)
  78.    {
  79.       this._tileGrid[tile._y * this._trayExtentX + tile._x] = iGroup;
  80.       this._arrGroups[iGroup].push(tile);
  81.       if(this.GetGroupSize(iGroup) >= this.GetDesiredGroupSize(iGroup))
  82.       {
  83.          return undefined;
  84.       }
  85.       var _loc5_ = new Array();
  86.       _loc5_.push(new Vector2D(tile._x + 1,tile._y + 0));
  87.       _loc5_.push(new Vector2D(tile._x - 1,tile._y + 0));
  88.       _loc5_.push(new Vector2D(tile._x + 0,tile._y + 1));
  89.       _loc5_.push(new Vector2D(tile._x + 0,tile._y - 1));
  90.       _loc5_.sort(this.CompareNeighborsRandomly);
  91.       var _loc3_ = 0;
  92.       while(_loc3_ < _loc5_.length)
  93.       {
  94.          var _loc2_ = _loc5_[_loc3_];
  95.          if(!(_loc2_._x < 0 || _loc2_._y < 0 || _loc2_._x >= this._trayExtentX || _loc2_._y >= this._trayExtentY))
  96.          {
  97.             if(this._tileGrid[_loc2_._y * this._trayExtentX + _loc2_._x] < 0)
  98.             {
  99.                this.AddToGroup(_loc2_,iGroup);
  100.                if(this.GetGroupSize(iGroup) >= this.GetDesiredGroupSize(iGroup))
  101.                {
  102.                   return undefined;
  103.                }
  104.             }
  105.          }
  106.          _loc3_ = _loc3_ + 1;
  107.       }
  108.    }
  109.    function PickRandomDesiredGroupSize(iGroup)
  110.    {
  111.       var _loc3_ = _root.random.GetRandom();
  112.       Math.pow(_loc3_,5);
  113.       this._desiredGroupSize[iGroup] = this._minGroupSize + _loc3_ * (this._maxGroupSize - this._minGroupSize);
  114.    }
  115.    function GetGroupSize(iGroup)
  116.    {
  117.       return this._arrGroups[iGroup].length;
  118.    }
  119.    function GetDesiredGroupSize(iGroup)
  120.    {
  121.       return this._desiredGroupSize[iGroup];
  122.    }
  123.    function EstablishGroup(startTile, iGroup)
  124.    {
  125.       this.PickRandomDesiredGroupSize(iGroup);
  126.       this.AddToGroup(startTile,iGroup);
  127.    }
  128. }
  129.